package team.bluepen.order.data.entity;

import space.lingu.light.DataColumn;
import space.lingu.light.DataTable;
import space.lingu.light.PrimaryKey;

import java.io.Serializable;
import java.util.Objects;

/**
 * 用户对象
 * @author Kai
 */
@DataTable(tableName = "user")
public class User implements Serializable {
    /** 用户ID */
    @PrimaryKey(autoGenerate = true)
    @DataColumn(name = "user_id")
    private long id;

    /** 用户名 */
    @DataColumn
    private String username;

    /** 用户密码 */
    @DataColumn(name = "user_password")
    private String password;

    /** 用户身份 */
    @DataColumn(name = "user_role")
    private Role role;

    public User(){
    }

    public User(String username, String password, long id, Role role) {
        this.username = username;
        this.password = password;
        this.id = id;
        this.role = role;
    }

    public User(String username, String password, long id, String role) {
        this.username = username;
        this.password = password;
        this.id = id;
        this.role = Role.get(role);
    }

    public String getUsername() {
        return username;
    }

    public User setUsername(String username) {
        this.username = username;
        return this;
    }

    public String getPassword() {
        return password;
    }

    public User setPassword(String password) {
        this.password = password;
        return this;
    }

    public long getId() {
        return id;
    }

    public User setId(long id) {
        this.id = id;
        return this;
    }

    public Role getRole() {
        return role;
    }

    public User setRole(Role role) {
        this.role = role;
        return this;
    }

    public User build() {
        return this;
    }

    private boolean check() {
        return id != 0 && password != null
                && username != null && role != null;
    }

    @Override
    public String toString() {
        return "User{" +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", role=" + role +
                ", id=" + id +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        User user = (User) o;
        return id == user.id
                && Objects.equals(username, user.username)
                && Objects.equals(password, user.password)
                && role == user.role;
    }

    @Override
    public int hashCode() {
        return Objects.hash(username, password, role, id);
    }


}
